if (window.plus) {
    ready();
} else {
    document.addEventListener('plusready', ready, false);
}

$(function () {
    if (AGENT_CLIENT.WEB) {
        ready();
    }
})

function ready() {
    var memberid = null;
    var registerPage = function () {
        this.init();
    };
    registerPage.prototype = {
        init: function () {
            this.initData();
            this.bindEvent();
        },
        initData: function () {
            memberid = getUrlParam("memberid");
        },
        bindEvent: function () {
            
            $("#phone").focus();
            
            //点击后退
            $("#back").on("click", function () {
                back();
            });

            //点击登陆
            $("#login").on("click", function () {
                redirect('login.html');
            });

            //点击获取验证码
            $("#getVcode").on("click", function () {
            	if( !isTelephone($("#phone").val()) ){
            		$.alert("请填写手机号");
            		return;
            	}
            	$.ajax({
            		type:"POST",
            		url: CONFIG_MANAGER.RequstUrl + "v1/account/check",
            		async: true,
            		contentType: 'application/json',
            		data: $("#phone").val(),
            	}).done(function(data){
            		console.log(data);
            		if(data.succeed){
            			$.alert('账号已经存在');
            		}else{
            			change_code($("#getVcode"), $("#phone"), SMS_TYPE.verify);
            		}
            	}).fail(function(data){
            		var infor = JSON.parse(data.responseText);
            		$.toast(infor.Message);
            	});
            });

            $("#phone,#vcode,#password,#confirmPassword").on("input", function () {
                if ("" != $("#phone").val() &&
                    "" != $("#vcode").val() &&
                    "" != $("#password").val() &&
                    "" != $("#confirmPassword").val()) {
                    $("#register").addClass("liang");
                } else {
                    $("#register").removeClass("liang");

                }
            });
            
            //点击注册
            $("#register").on("click", function () {
              if( !$("#register").hasClass("liang") ){
                return;
              }
              if(!isVCode($("#vcode").val())){
                  $.alert("请填写正确的验证码");
                  return false;              	
              }
              if (!isTelephone($("#phone").val())) {
                  $.alert("请填写正确的手机号");
                  return false;
              }
              if ($("#password").val() != $("#confirmPassword").val()) {
                  $.alert("两次填写的密码不一致");
                  return false;
              }
              if(!isPasswd($("#password").val())){
                  $.alert("密码为6至15位数字字母或下划线");
                  return false;              	
              }

              var postData = {
                  "phone": $("#phone").val(),
                  "password": $("#password").val(),
                  "ip": $.getIPAddress(),
                  "type": 3,
                  "vcode": $("#vcode").val()
              };
              if (memberid) {
                  postData.referrer = memberid;
                  postData.from = "Vaecn";
              }
              $.ajax({
                  type: "POST",
                  url: CONFIG_MANAGER.RequstUrl + "v1/account/register",
                  async: true,
                  data: postData
              }).always(function (data) {
                  if (data.status && 400 == data.status) {
                      $.alert(JSON.parse(data.responseText).Message);
                  } else {
                      if (data.id) {
                        if(AGENT_CLIENT.APP){
                          setLocalStorage(STORAGE_MANAGER.user, data);
                          plus.webview.getWebviewById("../mine/mylife.html").evalJS("reloadPage()");
                        }
                        $.alert("注册成功", function () {
                            redirect("registerSucc.html");
                        });
                      }
                  }
              });
          });
        },
    };

    var page = new registerPage();

}
